package training.osms.model.category;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@Table(name="CATEGORY", uniqueConstraints=@UniqueConstraint(columnNames="CAT_NAME"))
public class Category implements Cloneable{

	private Integer identifier;
	private String name;
	private String description;
	private Category parent;
	private List<Category> children;
	
	public Category(){
		this.name = "";
		this.description = "";
	}

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="CAT_IDENTIFIER")
	public Integer getIdentifier() {
		return identifier;
	}

	public void setIdentifier(Integer identifier) {
		this.identifier = identifier;
	}

	@NotNull
	@Column(name="CAT_NAME")
	@Size(max=60, min=3, message="The name must be between 3 and 60 characters.")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name="CAT_DESCRIPTION")
	@Size(max=200, message="The name must be less than 100 characters.")
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public Category clone(){
		
		try {
			return (Category)super.clone();
		} catch (CloneNotSupportedException e) {
			throw new IllegalAccessError("BUG DO MAL");
		}
		
		
	}

	@ManyToOne
	@JoinColumn(name="CAT_PARENT")
	public Category getParent() {
		return parent;
	}

	public void setParent(Category parent) {
		this.parent = parent;
	}

	@OneToMany(mappedBy="parent")
	public List<Category> getChildren() {
		return children;
	}

	public void setChildren(List<Category> children) {
		this.children = children;
	}	
	
	
	
	
}
